package com.yunji.framework_template.web.controller.api;

import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yunji.framework_template.biz.cache.CacheService;
import com.yunji.framework_template.common.util.ListUtil;
import com.yunji.framework_template.orm.persistence.model.Game;
import com.yunji.framework_template.orm.persistence.vo.DataDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * Created by fenglibin on 2018/12/12.
 */
@Slf4j
@RestController
@Controller
@RequestMapping("/api/game")
@Api(value = "游戏接口", tags = "gameAPI", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class GameApiController {

    @Resource
    private CacheService cacheService;

    @ApiImplicitParams({ @ApiImplicitParam(paramType = "path", dataType = "Int", name = "currentPage", value = "当前页的页码", required = true, defaultValue = "1") })
    @ApiOperation(value = "获取分页游戏的接口", notes = "如果分页数超过了总记录数，返回空数据", httpMethod = "GET")
    @RequestMapping(value = "/getMore/{currentPage}", method = { RequestMethod.GET, RequestMethod.POST })
    public DataDTO<List<Game>> getLatestNews(@PathVariable("currentPage") int currentPage) {
        log.info("获取最新的游戏，当前页是:" + currentPage);
        try {
            List<Game> gameList = cacheService.getCountryCache().getPageGame().get(currentPage);
            if(gameList!=null) {
                gameList = ListUtil.randomSort(gameList);
            }
            return DataDTO.success(gameList);
        } catch (Exception e) {
            log.error("Get game fail:" + e.getMessage(), e);
            return DataDTO.fail(500, "Get game fail.");
        }
    }
}
